/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * frmAddUser.java
 *
 * Created on 04/09/2012, 15:47:47
 */
package br.com.petshop.dao.frames;

import br.com.petshop.dao.ProdutoDAO;
import br.com.petshop.dao.UsuarioDAO;
import br.com.petshop.entidades.Produto;
import java.sql.SQLException;
import javax.swing.JOptionPane;

/**
 *
 * @author Alexandre
 */
public class frmAtualizarProdutos extends javax.swing.JDialog {

    public final ProdutoDAO prodDAO;
    public final Produto prodInEdit;//aqui a instancia do Produto vira atributo da classe.

    /** Creates new form frmAddUser */
    public frmAtualizarProdutos(java.awt.Frame parent, boolean modal, ProdutoDAO prodDAO, Produto prodInEdit) {
        super(parent, modal);
        this.prodDAO = prodDAO;
        this.prodInEdit = prodInEdit;
        initComponents();
        initMyComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jl_Nome = new javax.swing.JLabel();
        jl_Preco = new javax.swing.JLabel();
        jl_Estoque = new javax.swing.JLabel();
        tf_Nome = new javax.swing.JTextField();
        tf_Preco = new javax.swing.JTextField();
        tf_Estoqe = new javax.swing.JTextField();
        jb_Atualizar = new javax.swing.JButton();
        jb_Canc = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Atualizar Produto");
        setIconImage(null);
        setResizable(false);

        jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        jl_Nome.setText("Nome: ");

        jl_Preco.setText("Preco: ");

        jl_Estoque.setText("Estoque: ");

        jb_Atualizar.setText("Atualizar");
        jb_Atualizar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jb_AtualizarActionPerformed(evt);
            }
        });

        jb_Canc.setText("Cancelar");
        jb_Canc.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jb_CancActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jl_Nome)
                            .addComponent(jl_Estoque))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(tf_Estoqe)
                            .addComponent(tf_Preco)
                            .addComponent(tf_Nome, javax.swing.GroupLayout.DEFAULT_SIZE, 188, Short.MAX_VALUE))
                        .addContainerGap(108, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(jb_Atualizar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jb_Canc)
                        .addGap(128, 128, 128))
                    .addComponent(jl_Preco)))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tf_Nome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jl_Nome))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jl_Preco, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tf_Preco, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(13, 13, 13)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tf_Estoqe, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jl_Estoque))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jb_Atualizar)
                    .addComponent(jb_Canc))
                .addContainerGap(37, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(34, Short.MAX_VALUE)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(33, 33, 33))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(29, 29, 29)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(33, Short.MAX_VALUE))
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-441)/2, (screenSize.height-287)/2, 441, 287);
    }// </editor-fold>//GEN-END:initComponents

    private void jb_CancActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jb_CancActionPerformed
        setVisible(false);
    }//GEN-LAST:event_jb_CancActionPerformed

    private void jb_AtualizarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jb_AtualizarActionPerformed

        int result = JOptionPane.showConfirmDialog(this, "Confirma?", "Atualizacao Do Produto?", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
        if (result == 2) {
            return;//constante 0 confirma, 2 cancela      
        }        //Colocar este trecho dentro de um bloco try por causa das conversoes Float e Integer       
        try {
            prodInEdit.setNome(tf_Nome.getText());
            prodInEdit.setPreco(Float.parseFloat(tf_Preco.getText()));
            prodInEdit.setEstoque(Integer.parseInt(tf_Estoqe.getText()));
            prodDAO.atualizarProduto(prodInEdit);
        } catch (SQLException ex) {
            ex.printStackTrace();
            JOptionPane.showMessageDialog(this, "Erro Ao Atualizar Produto!" + ex + JOptionPane.ERROR_MESSAGE);
        } catch (NumberFormatException n) {
            JOptionPane.showMessageDialog(this, "Impossivel Converter" + n);
        }

        setVisible(false);


    }//GEN-LAST:event_jb_AtualizarActionPerformed
    /**
     * @param args the command line arguments
    
    public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
    
    public void run() {
    frmAdicionarProdutos dialog = new frmAdicionarProdutos(new javax.swing.JFrame(), true,new ProdutoDAO());
    dialog.addWindowListener(new java.awt.event.WindowAdapter() {
    
    public void windowClosing(java.awt.event.WindowEvent e) {
    System.exit(0);
    }
    });
    dialog.setVisible(true);
    }
    });
    }*/
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel jPanel1;
    private javax.swing.JButton jb_Atualizar;
    private javax.swing.JButton jb_Canc;
    private javax.swing.JLabel jl_Estoque;
    private javax.swing.JLabel jl_Nome;
    private javax.swing.JLabel jl_Preco;
    private javax.swing.JTextField tf_Estoqe;
    private javax.swing.JTextField tf_Nome;
    private javax.swing.JTextField tf_Preco;
    // End of variables declaration//GEN-END:variables

    private void initMyComponents() {
        try {
            tf_Nome.setText(prodInEdit.getNome());
            tf_Preco.setText(String.valueOf(prodInEdit.getPreco()));
            tf_Estoqe.setText(String.valueOf(prodInEdit.getEstoque()));
            tf_Nome.requestFocus();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
